﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using zeroone.Madrona.Models;
using System.Web.Helpers;
using MongoDB.Bson;
using Newtonsoft.Json.Linq;

namespace zeroone.Madrona.Controllers
{
    public class RecordsController : ApiController
    {
        // GET api/Artists/
        // returns all artists
        public HttpResponseMessage Get(string model)
        {
            // todo: assert user in role
            var resp = new HttpResponseMessage();
            resp.Content = new StringContent(new Recordset(model).GetRecordset());
            resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return resp;
        }

        // GET api/Artists/5069f825cd4c1d590cddf206
        // returns artist with id = "5069f825cd4c1d590cddf206"
        public HttpResponseMessage Get(string model, string id)
        {
            // todo: assert user in role
            var resp = new HttpResponseMessage();
            resp.Content = new StringContent(new Recordset(model).GetRecordById(id));
            resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return resp;
        }

        // POST api/Artists
        // payload: JSON
        // create artist record per posted values
        // return new id(?)
        //public void Post([FromBody]string value)
        //public HttpResponseMessage Post([FromBody]JObject value)
        //{
        //    var response = new HttpResponseMessage();
        //    var x = 10;
        //    return response;
        //}

        public HttpResponseMessage Post(string model, HttpRequestMessage value)
        {
            // todo: assert user in role

            var data = value.Content.ReadAsStringAsync().Result; // using .Result here for simplicity...
            Dictionary<string, object> valueDictionary;
            try
            {
                valueDictionary = Json.Decode<Dictionary<string, object>>(data);
            }
            catch (Exception ex)
            {
                throw new FormatException("invalid JSON", ex);
            }
            if (valueDictionary == null)
                throw new ArgumentNullException("valueDictionary", "empty or invalid json posted");

            var resp = new HttpResponseMessage();
            resp.Content = new StringContent(new Recordset(model).CreateRecord(valueDictionary));
            resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return resp;
        }

        // PUT api/Artists/5069f825cd4c1d590cddf206
        // PAYLOAD: JSON
        // update artsist record with id = "5069f825cd4c1d590cddf206"
        // set content of record to payload
        /// <summary>
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        public void Put(string model, int id, [FromBody]string value)
        {
        }

        // DELETE api/Artists/5069f825cd4c1d590cddf206
        // PAYLOAD: none
        // delete artist record with id = "5069f825cd4c1d590cddf206"
        public HttpResponseMessage Delete(string model, string id)
        {
            // todo: assert user in role
            var resp = new HttpResponseMessage();
            resp.Content = new StringContent(new Recordset(model).DeleteRecordById(id));
            resp.StatusCode = HttpStatusCode.Gone;
            resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return resp;
        }
    }
}
